package com.zzb.text.controller;

import com.zzb.text.entity.User;
import com.zzb.text.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * Created by ZZB on 2017/6/16.
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/index")
    public String list(Model model){
        List<User> list = userService.list();
        model.addAttribute("list",list);
        return "/user/index";
    }

    @RequestMapping("/form")
    public String form(User user,Model model){
        String id = user.getId();
        if(id!=null){
            user = userService.get(id);
        }
        model.addAttribute("user",user);
        return "/user/form";
    }

    @RequestMapping("/save")
    public String save(User user){
        userService.save(user);
        return "redirect:/user/index";
    }

    @RequestMapping("/delete")
    public String delete(String id){
        userService.delete(id);
        return "redirect:/user/index";
    }

}
